﻿using Microsoft.EntityFrameworkCore;
using Model;
using Model.Models;
using Model.Models.Auto;
using Model.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace CodeFirst
{
    public class DbConnectionFactory : DbContext
    {
        //private string strConn = "server=47.107.41.10;database=EmptyDB;uid=root;pwd=Wxl222225*;";

        //private string strConn = "Server=47.107.41.10;Database=EmptyDB;Uid=root;Pwd=Wxl222225*;";
        //private string strConn = "server=.;database=OnLine;uid=sa;pwd=1234;";
        private string strConn = "server=47.103.119.27,1899;database=OnLine;uid=root;pwd=123456;";
        //private string strConn = "server=.;database=GongYeHui;uid=sa;pwd=1234;";


        #region 复制代码

        #region 系统表

        public DbSet<ErrorLog> ErrorLog { get; set; }
        public DbSet<OnLineMsg> OnLineMsg { get; set; }
        public DbSet<LoginLog> LoginLog { get; set; }
        public DbSet<ApiLog> BPMLog { get; set; }
        public DbSet<Account> Account { get; set; }
        public DbSet<AccountRole> AccountRole { get; set; }
        public DbSet<Menu> Menu { get; set; }
        public DbSet<MenuAction> MenuAction { get; set; }
        public DbSet<Role> Role { get; set; }
        public DbSet<RoleMenu> RoleMenu { get; set; }
        public DbSet<RoleMenuAction> RoleMenuAction { get; set; }
        public DbSet<SysConfig> SysConfig { get; set; }

        public DbSet<WXUser> User { get; set; }
        public DbSet<CodeCreateTable> CodeCreateTable { get; set; }

        #endregion

        #region 业务表 

      
        public DbSet<Sys_FilePool> Sys_FilePool { get; set; }
        public DbSet<Bas_File> Bas_File { get; set; }
      
        public DbSet<Sys_User> Sys_User { get; set; }
      

        #endregion

        #endregion


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //引入sql连接
           // optionsBuilder.UseMySql(strConn, ServerVersion.AutoDetect(strConn));
            optionsBuilder.UseSqlServer(strConn);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            #region 复制代码

            DateTime now = Convert.ToDateTime("2024-01-01 00:00:00");

            #region 系统表
            string str1 = "sys_";

            modelBuilder.Entity<OnLineMsg>().ToTable($"{str1}OnLineMsg").HasIndex(a => new { a.User1, a.User2 });

            modelBuilder.Entity<ErrorLog>().ToTable($"{str1}ErrorLog").HasIndex(a => new { a.CreateUserName });
            modelBuilder.Entity<LoginLog>().ToTable($"{str1}LoginLog").HasIndex(a => new { a.CreateUserName });
            modelBuilder.Entity<ApiLog>().ToTable($"{str1}ApiLog").HasIndex(a => new { a.CreateUserName });

            modelBuilder.Entity<Account>().ToTable($"{str1}Account").HasIndex(a => new { a.AccountNo });
            modelBuilder.Entity<Account>().ToTable($"{str1}Account").HasData(new List<Account>() {
                new Account (){
                    Name="系统管理员",
                    AccountNo="admin",
                    Pwd="vGjHvVtojoSOOyhqZCCnFQ==",// 初始化密码：admin123
                    IsSysTem=1,
                    PwdKey="",
                    Tel="",
                    EnableState=1,
                    Id="xitongguanliyuan",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统管理员"
                }
            });
            modelBuilder.Entity<Role>().ToTable($"{str1}Role").HasData(new List<Role>() {
                new Role (){
                    RoleName="系统管理员",
                    Sort=1,
                    Id="xitongguanliyuanrole",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统管理员"
                }
            });
            modelBuilder.Entity<AccountRole>().ToTable($"{str1}AccountRole").HasData(new List<AccountRole>() {
                new AccountRole (){
                    AccountId="xitongguanliyuan",
                    RoleId="xitongguanliyuanrole",
                    Id="xitongguanliyuanbindrole",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统管理员"
                }
            });


            modelBuilder.Entity<Menu>().ToTable($"{str1}Menu").HasData(new List<Menu>() {
                new Menu (){
                    MenuName="系统工具",
                    Code="system",
                    Icon="系统设置.png",
                    LinkUrl="",
                    ParentId="",
                    Level=1,
                    Sort=99,
                    EnableState=1,
                    Id="xitonggongju",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                new Menu (){
                    MenuName="菜单管理",
                    Code="system.menu",
                    Icon="el-icon-menu",
                    LinkUrl="/menu",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=1,
                    EnableState=1,
                    Id="caidanguanli",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                new Menu (){
                    MenuName="账号管理",
                    Code="system.account",
                    Icon="el-icon-s-custom",
                    LinkUrl="/account",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=2,
                    EnableState=1,
                    Id="zhanghaoguanlli",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                new Menu (){
                    MenuName="角色管理",
                    Code="system.role",
                    Icon="el-icon-s-check",
                    LinkUrl="/role",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=3,
                    EnableState=1,
                    Id="juesegaunli",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                //new Menu (){
                //    MenuName="权限管理",
                //    Code="system.power",
                //    Icon="el-icon-s-check",
                //    LinkUrl="/power",
                //    ParentId="xitonggongju",
                //    Level=2,
                //    Sort=4,
                //    EnableState=1,
                //    Id="quanxainguanli",
                //    IsDelete=false,
                //    CreateTime= now,
                //    CreateUser="",
                //    UpdateUser="",
                //    DeleteUser="",
                //    Remark="系统初始数据"
                //},
                new Menu (){
                    MenuName="登录日志",
                    Code="system.loginlog",
                    Icon="el-icon-s-grid",
                    LinkUrl="/loginlog",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=5,
                    EnableState=1,
                    Id="denglurizhi",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                new Menu (){
                    MenuName="系统错误日志",
                    Code="system.errorlog",
                    Icon="el-icon-s-grid",
                    LinkUrl="/errorlog",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=6,
                    EnableState=1,
                    Id="xitongcuowurizhi",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                new Menu (){
                    MenuName="第三方接口日志",
                    Code="system.apilog",
                    Icon="el-icon-s-grid",
                    LinkUrl="/apilog",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=7,
                    EnableState=1,
                    Id="disanfangjiekourizhi",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
                 new Menu (){
                    MenuName="代码生成工具",
                    Code="system.codecreate",
                    Icon="el-icon-s-grid",
                    LinkUrl="/codecreate",
                    ParentId="xitonggongju",
                    Level=2,
                    Sort=8,
                    EnableState=1,
                    Id="daimashengchenggongju",
                    IsDelete=false,
                    CreateTime= now,
                    CreateUser="",
                    UpdateUser="",
                    DeleteUser="",
                    Remark="系统初始数据"
                },
            });
            modelBuilder.Entity<MenuAction>().ToTable($"{str1}MenuAction");
            modelBuilder.Entity<RoleMenu>().ToTable($"{str1}RoleMenu");
            modelBuilder.Entity<RoleMenuAction>().ToTable($"{str1}RoleMenuAction");
            modelBuilder.Entity<SysConfig>().ToTable($"{str1}SysConfig");

            modelBuilder.Entity<WXUser>().ToTable($"{str1}WXUser");



            #endregion


            #region 业务表
            string qianzhui = "su_";
            string chanping = "p_";

           
            modelBuilder.Entity<Sys_FilePool>().ToTable("Sys_FilePool");
            modelBuilder.Entity<Bas_File>().ToTable("Bas_File");
           
            modelBuilder.Entity<Sys_User>().ToTable("Sys_User");
           

            #endregion


            #region 代码生成工具表集合  存放需要自动生成前端代码的表数据集合
            modelBuilder.Entity<CodeCreateTable>().ToTable("sys_codecreatetable");
            //modelBuilder.Entity<CodeCreateTable>().ToTable("sys_codecreatetable").HasData(new List<CodeCreateTable>() {
            //    new CodeCreateTable (){
            //        Name="Test1",//放表的实体类名称 约定死
            //        Remark="测试表",
            //        Id=Guid.NewGuid().ToString("N")
            //    },
            //     new CodeCreateTable (){
            //        Name="Test2",//放表的实体类名称 约定死
            //        Remark="测试表2",
            //        Id=Guid.NewGuid().ToString("N")
            //    },
            //});

            #endregion

            #endregion
        }
    }
}
